package at.ac.univie.zsu.aguataplan.ui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import org.apache.log4j.Logger;

import at.ac.univie.zsu.aguataplan.property.GeneralProperties;
import at.ac.univie.zsu.aguataplan.property.I18nProperties;
import at.ac.univie.zsu.aguataplan.property.ShortcutProperties;
import at.ac.univie.zsu.aguataplan.property.SystemProperties;
import at.ac.univie.zsu.aguataplan.property.UIProperties;
import at.ac.univie.zsu.aguataplan.service.Services;
import at.ac.univie.zsu.aguataplan.util.ExitHandler;
import at.ac.univie.zsu.aguataplan.video.util.IExit;

/**
 * @author gerry
 * 
 */
public class MainWindow extends JFrame implements UIConstants {
	private static Logger log = Logger.getLogger(MainWindow.class);
	private static final long serialVersionUID = 5742343937334043594L;

	// ----------------------------------------------

	private GeneralProperties generalProperties;
	private ShortcutProperties shortcutProp;
	private UIProperties uiProp;
	private SystemProperties systemProp;
	private I18nProperties i18nProp;

	private Services services;

	// ----------------------------------------------

	private VideoPlayerPanel videoPlayerPanel;

	private JPanel deskPanel;

	private JMenuBar menuBar;

	// FILE MENU
	private JMenu fileMenu;
	private JMenuItem quitItem;

	// CAPTURE MENU
	private JMenu captureMenu;
	private JMenuItem captureEventItem;
	private JMenuItem captureEventRelationItem;
	private JMenuItem captureClubItem;
	private JMenuItem captureCoachItem;
	private JMenuItem capturePlayerItem;
	private JMenuItem captureTeamItem;
	private JMenuItem captureWorkingDescriptionItem;
	private JMenuItem captureGameItem;

	// ANALYSE MENU
	private JMenu analyseMenu;
	private JMenuItem analyzeItem;
	private JMenuItem exportItem;

	// ABOUT MENU
	private JMenu aboutMenu;
	private JMenuItem licenseItem;
	private JMenuItem aboutItem;

	// dialog
	private DialogClub dialogClub;
	private DialogEvent dialogEvent;
	private DialogEventRelation dialogEventRelation;
	private DialogTeam dialogTeam;
	private DialogGame dialogGame;

	private DialogAnalyze dialogAnalyze;
	private DialogExport dialogExport;

	private List<IExit> exits;

	// ----------------------------------------------------
	// CONSTRUCTOR
	// ----------------------------------------------------

	public MainWindow(GeneralProperties generalProperties, Services services) {
		super();
		setLookAndFeel();

		this.generalProperties = generalProperties;
		this.shortcutProp = generalProperties.getShortcutProperties();
		this.uiProp = generalProperties.getUiProperties();
		this.systemProp = generalProperties.getSystemProperties();
		this.i18nProp = generalProperties.getI18nProperties();

		this.services = services;

		exits = new ArrayList<IExit>();

		this.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				ExitHandler.exit(MainWindow.this, exits);
			}
		});
		this.setLayout(new BorderLayout());
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);

		deskPanel = new JPanel();
		deskPanel.setLayout(new BorderLayout());

		menuBar = new JMenuBar();

		fileMenu = new JMenu(i18nProp.getString(FILE_MENU));
		quitItem = new JMenuItem(i18nProp.getString(QUIT_ITEM));
		// quitItem.setMnemonic('q');
		quitItem.setAccelerator(KeyStroke.getKeyStroke(shortcutProp.getScGuiQuit().charAt(0),
				shortcutProp.getScGuiExtendedModifier()));
		fileMenu.add(quitItem);

		captureMenu = new JMenu(i18nProp.getString(CAPTURE_MENU));
		captureEventItem = new JMenuItem(i18nProp.getString(CAPTUREEVENT_ITEM));
		captureEventItem.setAccelerator(KeyStroke.getKeyStroke(shortcutProp.getScEvent().charAt(0),
				shortcutProp.getScGuiExtendedModifier()));
		captureEventRelationItem = new JMenuItem(i18nProp.getString(CAPTUREEVENTRELATION_ITEM));
		captureEventRelationItem.setAccelerator(KeyStroke.getKeyStroke(shortcutProp.getScEventRelation().charAt(0),
				shortcutProp.getScGuiExtendedModifier()));
		captureClubItem = new JMenuItem(i18nProp.getString(CAPTURECLUB_ITEM));
		captureClubItem.setAccelerator(KeyStroke.getKeyStroke(shortcutProp.getScClub().charAt(0),
				shortcutProp.getScGuiExtendedModifier()));
		captureCoachItem = new JMenuItem(i18nProp.getString(CAPTURECOACH_ITEM));
		captureCoachItem.setAccelerator(KeyStroke.getKeyStroke(shortcutProp.getScCoach().charAt(0),
				shortcutProp.getScGuiExtendedModifier()));
		capturePlayerItem = new JMenuItem(i18nProp.getString(CAPTUREPLAYER_ITEM));
		capturePlayerItem.setAccelerator(KeyStroke.getKeyStroke(shortcutProp.getScPlayer().charAt(0),
				shortcutProp.getScGuiExtendedModifier()));
		captureTeamItem = new JMenuItem(i18nProp.getString(CAPTURETEAM_ITEM));
		captureTeamItem.setAccelerator(KeyStroke.getKeyStroke(shortcutProp.getScTeam().charAt(0),
				shortcutProp.getScGuiExtendedModifier()));
		captureWorkingDescriptionItem = new JMenuItem(i18nProp.getString(CAPTUREWORKINGDESCRIPTION_ITEM));
		captureWorkingDescriptionItem.setAccelerator(KeyStroke.getKeyStroke(shortcutProp.getScWorkingDescription()
				.charAt(0), shortcutProp.getScGuiExtendedModifier()));
		captureGameItem = new JMenuItem(i18nProp.getString(CAPTUREGAME_ITEM));
		captureGameItem.setAccelerator(KeyStroke.getKeyStroke(shortcutProp.getScGame().charAt(0),
				shortcutProp.getScGuiExtendedModifier()));
		captureMenu.add(captureEventItem);
		captureMenu.add(captureEventRelationItem);
		captureMenu.add(captureClubItem);
		// captureMenu.add(captureCoachItem);
		// captureMenu.add(capturePlayerItem);
		captureMenu.add(captureTeamItem);
		// captureMenu.add(captureWorkingDescriptionItem);
		captureMenu.add(captureGameItem);

		analyseMenu = new JMenu(i18nProp.getString(ANALYSE_MENU));
		analyzeItem = new JMenuItem(i18nProp.getString(ANALYSE_ITEM));
		analyzeItem.setAccelerator(KeyStroke.getKeyStroke(shortcutProp.getScAnalyze().charAt(0),
				shortcutProp.getScGuiExtendedModifier()));
		exportItem = new JMenuItem(i18nProp.getString(EXPORT_ITEM));
		exportItem.setAccelerator(KeyStroke.getKeyStroke(shortcutProp.getScExport().charAt(0),
				shortcutProp.getScGuiExtendedModifier()));
		analyseMenu.add(analyzeItem);
		analyseMenu.add(exportItem);

		aboutMenu = new JMenu(i18nProp.getString(HELP_MENU));
		licenseItem = new JMenuItem(i18nProp.getString(LICENSE_ITEM));
		aboutItem = new JMenuItem(i18nProp.getString(ABOUT_ITEM));
		aboutMenu.add(licenseItem);
		aboutMenu.add(aboutItem);

		menuBar.add(fileMenu);
		menuBar.add(captureMenu);
		menuBar.add(analyseMenu);
		menuBar.add(aboutMenu);

		setJMenuBar(menuBar);

		add(deskPanel);

		// ActionListener
		// --------------------------

		// FILE MENU:

		quitItem.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				ExitHandler.exit(MainWindow.this, exits);
			}
		});

		// CAPTURE MENU:

		captureEventItem.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				dialogEvent = new DialogEvent(MainWindow.this, MainWindow.this.generalProperties);
				dialogEvent.setVisible(true);

			}
		});

		captureEventRelationItem.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				dialogEventRelation = new DialogEventRelation(MainWindow.this, MainWindow.this.generalProperties);
				dialogEventRelation.setVisible(true);
			}
		});

		captureClubItem.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				dialogClub = new DialogClub(MainWindow.this, MainWindow.this.generalProperties);
				dialogClub.setVisible(true);

			}
		});

		captureTeamItem.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				dialogTeam = new DialogTeam(MainWindow.this, MainWindow.this.generalProperties);
				dialogTeam.setVisible(true);
			}
		});

		captureGameItem.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				dialogGame = new DialogGame(MainWindow.this, MainWindow.this.generalProperties);
				dialogGame.setVisible(true);
			}
		});

		// ANALYSE MENU:

		analyzeItem.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				dialogAnalyze = new DialogAnalyze(MainWindow.this, MainWindow.this.generalProperties);
				dialogAnalyze.setVisible(true);
			}
		});

		exportItem.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				dialogExport = new DialogExport(MainWindow.this, MainWindow.this.generalProperties);
				dialogExport.setVisible(true);

			}
		});

		// ABOUT MENU:

		licenseItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(MainWindow.this, MainWindow.this.uiProp.getLicense(),
						MainWindow.this.i18nProp.getString(LICENSE_NAME), JOptionPane.INFORMATION_MESSAGE);

			}
		});

		aboutItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(
						MainWindow.this,
						i18nProp.getString(ABOUT_TEXT) + "\n" + i18nProp.getString(ABOUT_VERSION) + ": "
								+ systemProp.getVersion() + "\n" + i18nProp.getString(ABOUT_VENDOR) + ": "
								+ systemProp.getVendor() + "\n" + i18nProp.getString(ABOUT_HOMEPAGE) + ": "
								+ systemProp.getHomepage(), MainWindow.this.i18nProp.getString(ABOUT_NAME),
						JOptionPane.INFORMATION_MESSAGE);
			}
		});

		log.debug(MainWindow.class.getSimpleName() + " started...");
		this.pack();
		this.defineSize();
		this.setVisible(true);

		setIconImage(new ImageIcon(uiProp.getIconPath() + ICON_HEAD).getImage());
		this.setTitle(uiProp.getTitle());

	}

	// ----------------------------------------------------
	// METHODS
	// ----------------------------------------------------

	private void defineSize() {
		this.setSize(this.uiProp.getSizeX(), this.uiProp.getSizeY());
		this.setResizable(false);
	}

	public void addExit(IExit iExit) {
		this.exits.add(iExit);
	}

	private void setLookAndFeel() {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (ClassNotFoundException e) {
			log.error(e.toString());
		} catch (InstantiationException e) {
			log.error(e.toString());
		} catch (IllegalAccessException e) {
			log.error(e.toString());
		} catch (UnsupportedLookAndFeelException e) {
			log.error(e.toString());
		}
		log.info("Look and feel changed!");
	}

	// ----------------------------------------------------
	// GETTER & SETTER
	// ----------------------------------------------------

	public Services getServices() {
		return services;
	}
}
